package com.dyh.controller;

import com.dyh.po.institude;
import com.dyh.service.insService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class insController {
    @Autowired
    insService insservice;

    @RequestMapping(value = "agrInsList")
    public String insList(Model model){
        List<institude> inss = insservice.findAllInst();
        System.out.println(inss);
        model.addAttribute("InsList", inss);
        return "institute/instlist";
    }


    @RequestMapping(value = "instEdit")
    public ModelAndView toUserEdit(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        String instname = request.getParameter("i_name");
        institude institude = insservice.findInstByName(instname);
        mv.addObject("Institude",institude);
        mv.setViewName("institute/instEdit");
        return mv;
    }

    /**
     * 修改用户
     * @param request
     * @return String
     */
    @RequestMapping(value = "instEditAction")
    public String editUser(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        Long id = Long.valueOf(request.getParameter("id"));
        System.out.println("id="+id);
        String instname = request.getParameter("instname");
        String areaid = request.getParameter("areaid");
        int row = insservice.editInst(id,instname,areaid);
        System.out.println(row);
        if(row>0){
            return "redirect:/agrInsList";
        }else{
            return "institute/instEdit";
        }
    }


    @RequestMapping(value = "agrInsAdd")
    public String toinstAdd(){
        return "institute/instAdd";
    }


    @RequestMapping(value = "instAddAction")
    public String addinst(HttpServletRequest request,Model model){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String instid = request.getParameter("instid");
        String instname = request.getParameter("instname");
        String instlevel = request.getParameter("instlevel");
        String instfather = request.getParameter("instfather");
        String areaid = request.getParameter("areaid");
        int row=insservice.addInst(instid,instname,instlevel,instfather,areaid);
        System.out.println("row="+row);
        if(row>0){
            return "/institute/instlist";
        }else {
            return "institute/instAdd";
        }
    }

    @RequestMapping(value = "instDeleteAction")
    public String deleteInst(HttpServletRequest request){
        Long id = Long.valueOf(request.getParameter("id"));
        System.out.println("id="+id);
        int row = insservice.deleteInst(id);
        return "redirect:/agrInsList";
    }
}
